//
//  BiBaseViewController.m
//  CodoonBikeApp
//
//  Created by ChenBin on 14-3-28.
//  Copyright (c) 2014年 Nahan Cop. All rights reserved.
//

#import "BiBaseViewController.h"
#import "BiTipsView.h"
#import "BiAlertView.h"

@interface BiBaseViewController ()<BiAlertViewDelegate>
@property (nonatomic, retain) NSTimer * tipsTimer;
@end

@implementation BiBaseViewController
@synthesize tipsTimer;
//@synthesize service = _service;

- (id)init
{
    self = [super init];
    if (self)
    {
        if(isIOS7)
        {
            _topMargin = 20;
            
            // Custom initialization
//            _service = [[BiService alloc] init];
//            [_service setDelegate:self];
        }
    }
    return self;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    _naviBarView = [[BiNaviBarView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [self.view addSubview:_naviBarView];
    [_naviBarView setHidden:NO];
    if (isIOS7)
    {
        [_naviBarView setFrame:CGRectMake(0, 0, _naviBarView.frame.size.width, 64)];
    }
    else
    {
        [_naviBarView setFrame:CGRectMake(0, -20, _naviBarView.frame.size.width, 64)];
    }
    [_naviBarView release];
    

}
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    
//    [self.view bringSubviewToFront:_naviBarView];
//}

- (void)dealloc
{
    if (nil != self.tipsTimer)
    {
        [self.tipsTimer invalidate];
        self.tipsTimer = nil;
    }
    
//    _service.delegate = nil;
//    _RELEASE(_service)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setNavBarWithHidden:(BOOL)hidden
{
    [_naviBarView setHidden:hidden];
    
    if (hidden)
    {
        [_naviBarView setFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
    }
    else
    {
        [self.view bringSubviewToFront:_naviBarView];
        
        if (isIOS7)
        {
            [_naviBarView setFrame:CGRectMake(0, 0, _naviBarView.frame.size.width, 64)];
        }
        else
        {
            [_naviBarView setFrame:CGRectMake(0, -20, _naviBarView.frame.size.width, 64)];
        }
    }
}

- (void)setNavBarTitle:(NSString *)title
{
    [_naviBarView setTitle:title];
}

- (void)setNavBarTitleImage:(UIImage*)image
{
    [_naviBarView setTitleImage:image];
}

- (void)setNavBackBtn
{
    [self setNavLeftButton:LOAD_IMAGE_WITH_CACHE(@"common/icon_main_back.png") target:self action:@selector(onNavBackBtnClick:)];
}

- (void)setNavLeftButton:(UIImage *)icon target:(id)target action:(SEL)action
{
    [_naviBarView setLeftButton:icon target:target action:action];
}
- (void)setNavRightButton:(UIImage *)icon target:(id)target action:(SEL)action
{
    [_naviBarView setRightButton:icon target:target action:action];
}
- (void)setNavRightButtonTitle:(NSString*)title target:(id)target action:(SEL)action
{
    [_naviBarView setRightButtonTitle:title target:target action:action];
}
- (void)removeNavRightBtn
{
    [_naviBarView setRightButtonTitle:@"" target:nil action:nil];
}

- (void)onNavBackBtnClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (int)viewTop
{
    return (_naviBarView.frame.origin.y + _naviBarView.frame.size.height);
}

- (void)addTestView
{
    UIView * testView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:testView];
    [testView release];
}

- (void)showTipsView:(NSString*)tipText
{
    BiTipsView * tipsView = (BiTipsView *)[self.view viewWithTag:TIP_VIEW_TAG];
    if (nil == tipsView)
    {
        CGRect screenBounds = [UIScreen mainScreen].bounds;
        tipsView = [[BiTipsView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
        [tipsView setTag:TIP_VIEW_TAG];
        [self.view addSubview:tipsView];
        [tipsView release];
    }
    
    [tipsView setTipText:tipText];
    [self.view bringSubviewToFront:tipsView];
    
    self.tipsTimer = [NSTimer scheduledTimerWithTimeInterval:1.8 target:self selector:@selector(removeTipsView) userInfo:nil repeats:NO];
}

- (void)removeTipsView
{
    self.tipsTimer = nil;
    
    BiTipsView * tipsView = (BiTipsView *)[self.view viewWithTag:TIP_VIEW_TAG];
    if (nil != tipsView)
    {
        [tipsView removeFromSuperview];
    }
}

- (void)showAlertView:(NSString*)tipText
{
    BiAlertView * alertView = (BiAlertView *)[self.view viewWithTag:TIP_VIEW_TAG];
    if (nil == alertView)
    {
        CGRect screenBounds = [UIScreen mainScreen].bounds;
        alertView = [[BiAlertView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
        [alertView setTag:ALERT_VIEW_TAG];
        [self.view addSubview:alertView];
        alertView.delegate = self;
        [alertView release];
    }
    
    [alertView setTipText:tipText];
    [self.view bringSubviewToFront:alertView];
}

- (void)onAlertViewOkBtnClick
{
    
}

- (BOOL)isConnectToNetwork
{
    if (![BiUtil isConnectToNetwork])
    {
        [self showTipsView:@"亲，你的网络不给力。"];
        return NO;
    }
    
    return YES;
}


@end
